layui.use(['form', 'layer'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;


    /**
     * 监听表单submit事件
     *     form.on('submit(addOrUpdateSaleChance)',function (data) {
    });
     */
    form.on('submit(addOrUpdateSaleChance)', function (data) {
        var index = layer.msg("数据提交中，请稍等...", {
            icon: 16,//图标
            time: false,//不关闭
            shade: 0.8 //设置遮罩的透明度
        });

        //发送ajax请求
        var url = ctx + "/sale_chance/add"; //添加操作

        var saleChanceId = $("[name='id']").val();

        if (saleChanceId != null && saleChanceId != '') {
            url = ctx + "/sale_chance/update"; //更新操作
        }

        $.post(url, data.field, function (result) {

            if (result.code == 200) {
                //成功
                layer.msg("操作成功", {icon: 6});
                //关闭加载层
                layer.close(index);
                //关闭弹出层
                layer.closeAll("iframe");
                //刷新父窗口，重新加载数据表格
                parent.location.reload();
            } else {
                //失败
                layer.msg(result.msg, {icon: 5});
                layer.close(index);
            }
        });

        //阻止表单提交
        return false;
    });


    /**
     * 关闭弹出层
     */
    $("#closeBtn").click(function () {
        // 当你在iframe页面关闭自身时
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    });

    $.ajax({
        type: "get",
        url: ctx + "/user/query",
        data: {},
        success: function (data) {
            //获取隐藏域设置的指派人id
            var assignManId = $("#assignManId").val();
            //遍历循环得到的数组
            if (data != null) {
                var opt = "";
                for (var i = 0; i < data.length; i++) {
                    if (assignManId == data[i].id) {
                        opt = "<option value='" + data[i].id + "' selected>" + data[i].uname + "</option>"
                    } else {
                        opt = "<option value='" + data[i].id + "'>" + data[i].uname + "</option>"
                    }
                    $("#assignMan").append(opt);
                }
            }
            //重新渲染下拉框
            layui.form.render("select");
        }
    });

});